module S2(clk,
	  rst,
	  updown,
	  S2_done,
	  RB2_RW,
	  RB2_A,
	  RB2_D,
	  RB2_Q,
	  sen,
	  sd);

  input clk,
        rst,
        updown;
  
  output S2_done,
         RB2_RW;
  
  output [2:0] RB2_A;
  
  output [17:0] RB2_D;
  
  input [17:0] RB2_Q;
  
  inout sen,
        sd;

reg S2_done;
reg RB2_RW;
reg [2:0] RB2_A;
reg [17:0] RB2_D;
reg sd_temp_s2;
reg sen_temp_s2;
reg [4:0] en_21;
reg [4:0]count_init;
reg [17:0] S2_RQ [7:0];
reg [3:0] count_X;
reg [4:0] count_Y;

/*
wire [17:0] S2_RQ_0;
wire [17:0] S2_RQ_1;
wire [17:0] S2_RQ_2;
wire [17:0] S2_RQ_3;
wire [17:0] S2_RQ_4;
wire [17:0] S2_RQ_5;
wire [17:0] S2_RQ_6;
wire [17:0] S2_RQ_7;

assign S2_RQ_0 = S2_RQ[0];
assign S2_RQ_1 = S2_RQ[1];
assign S2_RQ_2 = S2_RQ[2];
assign S2_RQ_3 = S2_RQ[3];
assign S2_RQ_4 = S2_RQ[4];
assign S2_RQ_5 = S2_RQ[5];
assign S2_RQ_6 = S2_RQ[6];
assign S2_RQ_7 = S2_RQ[7];
*/


assign sd = (updown)?sd_temp_s2:1'bz;
assign sen = (updown)?sen_temp_s2:1'bz;


//count_init
always@(posedge clk or posedge rst)
  begin
    if(rst)
      count_init <= 0;  
    else if (count_init == 19)
      count_init <= 0;
    else 
      count_init <= count_init + 1'b1;
  end
//s2_done
always@(posedge clk or posedge rst)
  begin
    if(rst)
      S2_done <= 0;
    else if (RB2_A==7 && ~RB2_RW && en_21==2) 
      S2_done <= 1; 
  end

//RB2_A
always@(negedge clk or posedge rst)
  begin
    if(rst)
      RB2_A <= 0;
    else if (updown)// && RB2_RW)
      RB2_A <= RB2_A + 1'b1;
 //   else if (updown)
 //     RB2_A <= 0;
    else if (en_21>1 && en_21<5)
      RB2_A[4-en_21] <= sd;
  end
//RB2_D
always@(negedge clk or posedge rst)
  begin
    if(rst)
      RB2_D <= 0;
    else if (en_21>4)
      RB2_D[22-en_21] <= sd;
  end

//S2_RQ
//always@(negedge clk or posedge rst)
always@(negedge clk)
  begin
    if(updown && en_21<11)
      S2_RQ[RB2_A] <= RB2_Q;
  end


//rb2_rw
always@(negedge clk or posedge rst)
  begin
    if(rst)
      RB2_RW <= 1;
    else if (updown)
      RB2_RW <= 1;
    else if(sen)
      RB2_RW <= 0;
    else
      RB2_RW <= 1;
  end
//en_21
always@(negedge clk or posedge rst)
  begin
    if(rst)
      en_21 <= 0;
    //else if (count_init== 19)
    else if (count_init== 19 && en_21==0)
      en_21 <= 1;
    else if (en_21 == 22)
      en_21 <= 1;
    else if (en_21 > 0) 
      en_21 <= en_21 + 1'b1;
  end



//count_array_X 
always@(posedge clk or posedge rst)
  begin
    if(rst)
      count_X <= 15;
    else if (count_X == 0)
      count_X <=  13;
    else if (updown)
      count_X <= count_X - 1'b1;
  end
//count_array_Y 
always@(posedge clk or posedge rst)
  begin
    if(rst)
      count_Y <= 17;
    else if (count_Y == 0)
      count_Y <= 0;
    else if (count_X == 0)
      count_Y <= count_Y - 1'b1;
  end


  
//sen_temp_s2
always@(posedge clk or posedge rst)
  begin
    if(rst)
      sen_temp_s2 <= 1;
    else if (count_X == 14)
      sen_temp_s2 <= 1;
    //else if (count_X == 12 && count_Y==17)
      //sen_temp_s2 <= 1;
    else if (count_X == 12 )
      sen_temp_s2 <= 0;
    else if (count_X == 13 && updown )
      sen_temp_s2 <= 1;
  end
//sd_temp_s2
always@(posedge clk or posedge rst)
  begin
    if(rst)
      sd_temp_s2<=0;
    else if (count_X < 8)
      sd_temp_s2 <= S2_RQ[count_X][count_Y];
    else if (count_X == 12)begin
      case(count_Y)
       17:sd_temp_s2<= 0 ;
       16:sd_temp_s2<= 0 ;
       15:sd_temp_s2<= 0 ;
       14:sd_temp_s2<= 0 ;
       13:sd_temp_s2<= 0 ;
       12:sd_temp_s2<= 0 ;
       11:sd_temp_s2<= 0 ;
       10:sd_temp_s2<= 0 ;
        9:sd_temp_s2<= 0 ;
        8:sd_temp_s2<= 0 ;
        7:sd_temp_s2<= 0 ;
        6:sd_temp_s2<= 0 ;
        5:sd_temp_s2<= 0 ;
        4:sd_temp_s2<= 0 ;
        3:sd_temp_s2<= 0 ;
        2:sd_temp_s2<= 0 ;
        1:sd_temp_s2<= 1 ;
        0:sd_temp_s2<= 1 ;
      endcase
    end
    else if (count_X == 11)begin
      case(count_Y)
       17:sd_temp_s2<= 0 ;
       16:sd_temp_s2<= 0 ;
       15:sd_temp_s2<= 0 ;
       14:sd_temp_s2<= 0 ;
       13:sd_temp_s2<= 0 ;
       12:sd_temp_s2<= 0 ;
       11:sd_temp_s2<= 0 ;
       10:sd_temp_s2<= 0 ;
        9:sd_temp_s2<= 1 ;
        8:sd_temp_s2<= 1 ;
        7:sd_temp_s2<= 1 ;
        6:sd_temp_s2<= 1 ;
        5:sd_temp_s2<= 1 ;
        4:sd_temp_s2<= 1 ;
        3:sd_temp_s2<= 1 ;
        2:sd_temp_s2<= 1 ;
        1:sd_temp_s2<= 0 ;
        0:sd_temp_s2<= 0 ;
      endcase
    end
    else if (count_X ==10)begin
      case(count_Y)
       17:sd_temp_s2<= 0 ;
       16:sd_temp_s2<= 0 ;
       15:sd_temp_s2<= 0 ;
       14:sd_temp_s2<= 0 ;
       13:sd_temp_s2<= 1 ;
       12:sd_temp_s2<= 1 ;
       11:sd_temp_s2<= 1 ;
       10:sd_temp_s2<= 1 ;
        9:sd_temp_s2<= 0 ;
        8:sd_temp_s2<= 0 ;
        7:sd_temp_s2<= 0 ;
        6:sd_temp_s2<= 0 ;
        5:sd_temp_s2<= 1 ;
        4:sd_temp_s2<= 1 ;
        3:sd_temp_s2<= 1 ;
        2:sd_temp_s2<= 1 ;
        1:sd_temp_s2<= 0 ;
        0:sd_temp_s2<= 0 ;
      endcase
    end
    else if (count_X == 9 )begin
      case(count_Y)
       17:sd_temp_s2<= 0 ;
       16:sd_temp_s2<= 0 ;
       15:sd_temp_s2<= 1 ;
       14:sd_temp_s2<= 1 ;
       13:sd_temp_s2<= 0 ;
       12:sd_temp_s2<= 0 ;
       11:sd_temp_s2<= 1 ;
       10:sd_temp_s2<= 1 ;
        9:sd_temp_s2<= 0 ;
        8:sd_temp_s2<= 0 ;
        7:sd_temp_s2<= 1 ;
        6:sd_temp_s2<= 1 ;
        5:sd_temp_s2<= 0 ;
        4:sd_temp_s2<= 0 ;
        3:sd_temp_s2<= 1 ;
        2:sd_temp_s2<= 1 ;
        1:sd_temp_s2<= 0 ;
        0:sd_temp_s2<= 0 ;
      endcase
    end
    else if (count_X == 8 )begin
      case(count_Y)
       17:sd_temp_s2<= 0 ;
       16:sd_temp_s2<= 1 ;
       15:sd_temp_s2<= 0 ;
       14:sd_temp_s2<= 1 ;
       13:sd_temp_s2<= 0 ;
       12:sd_temp_s2<= 1 ;
       11:sd_temp_s2<= 0 ;
       10:sd_temp_s2<= 1 ;
        9:sd_temp_s2<= 0 ;
        8:sd_temp_s2<= 1 ;
        7:sd_temp_s2<= 0 ;
        6:sd_temp_s2<= 1 ;
        5:sd_temp_s2<= 0 ;
        4:sd_temp_s2<= 1 ;
        3:sd_temp_s2<= 0 ;
        2:sd_temp_s2<= 1 ;
        1:sd_temp_s2<= 0 ;
        0:sd_temp_s2<= 1 ;
      endcase
    end
  end

endmodule
/*
ERROR : The   0th download frame = 00000 01100000 (expect = 00000 00011111)
ERROR : The   1th download frame = 00000 01000100 (expect = 00001 11010000)
ERROR : The   2th download frame = 00000 01000100 (expect = 00010 10001000)
ERROR : The   3th download frame = 00000 01000011 (expect = 00011 10001000)
ERROR : The   4th download frame = 00000 01100100 (expect = 00100 10000111)
ERROR : The   5th download frame = 00000 01100000 (expect = 00101 11001000)
ERROR : The   6th download frame = 00000 00100010 (expect = 00110 11000000)
ERROR : The   7th download frame = 00000 01000111 (expect = 00111 01010111)
ERROR : The   8th download frame = 00000 01000011 (expect = 01000 10101111)
ERROR : The   9th download frame = 00000 01011110 (expect = 01001 10010101)
ERROR : The  10th download frame = 00000 01110000 (expect = 01010 10111111)
ERROR : The  11th download frame = 00000 01010011 (expect = 01011 11010010)
ERROR : The  12th download frame = 00000 01010100 (expect = 01100 10110111)
ERROR : The  13th download frame = 00000 00101010 (expect = 01101 10101111)
ERROR : The  14th download frame = 00000 00010100 (expect = 01110 01100000)
ERROR : The  15th download frame = 00000 00000101 (expect = 01111 00001101)
ERROR : The  16th download frame = 00000 00100100 (expect = 10000 00001010)
ERROR : The  17th download frame = 00000 00100100 (expect = 10001 01011101)

ERROR : RB1[00] = 57 (expect = a8)
ERROR : RB1[01] = cb (expect = 34)
ERROR : RB1[02] = bd (expect = 42)
ERROR : RB1[03] = 02 (expect = fd)
ERROR : RB1[04] = 57 (expect = a8)
ERROR : RB1[05] = c2 (expect = 3d)
ERROR : RB1[06] = 27 (expect = d8)
ERROR : RB1[07] = 8e (expect = 71)
ERROR : RB1[08] = f8 (expect = 07)
ERROR : RB1[09] = 98 (expect = 67)
ERROR : RB1[0a] = 10 (expect = ef)
ERROR : RB1[0b] = 1d (expect = e2)
ERROR : RB1[0c] = 98 (expect = 67)
ERROR : RB1[0d] = c0 (expect = 3f)
ERROR : RB1[0e] = 00 (expect = ff)
ERROR : RB1[0f] = 4d (expect = b2)
ERROR : RB1[10] = e4 (expect = 1b)
ERROR : RB1[11] = 27 (expect = d8)
ERROR : The   0th download frame = 00000 01010111 (expect = 00000 10101000)
ERROR : The   1th download frame = 00001 11001011 (expect = 00001 00110100)
ERROR : The   2th download frame = 00010 10111101 (expect = 00010 01000010)
ERROR : The   3th download frame = 00011 00000010 (expect = 00011 11111101)
ERROR : The   4th download frame = 00100 01010111 (expect = 00100 10101000)
ERROR : The   5th download frame = 00101 11000010 (expect = 00101 00111101)
ERROR : The   6th download frame = 00110 00100111 (expect = 00110 11011000)
ERROR : The   7th download frame = 00111 10001110 (expect = 00111 01110001)
ERROR : The   8th download frame = 01000 11111000 (expect = 01000 00000111)
ERROR : The   9th download frame = 01001 10011000 (expect = 01001 01100111)
ERROR : The  10th download frame = 01010 00010000 (expect = 01010 11101111)
ERROR : The  11th download frame = 01011 00011101 (expect = 01011 11100010)
ERROR : The  12th download frame = 01100 10011000 (expect = 01100 01100111)
ERROR : The  13th download frame = 01101 11000000 (expect = 01101 00111111)
ERROR : The  14th download frame = 01110 00000000 (expect = 01110 11111111)
ERROR : The  15th download frame = 01111 01001101 (expect = 01111 10110010)
ERROR : The  16th download frame = 10000 11100100 (expect = 10000 00011011)
ERROR : The  17th download frame = 10001 00100111 (expect = 10001 11011000)
RB2_7[17],RB2_6[17], RB2_5[17], RB2_4[17], RB2_3[17], RB2_2[17], RB2_1[17], RB2_1[17]
RB2_7[16],RB2_6[16], RB2_5[16], RB2_4[16], RB2_3[16], RB2_2[16], RB2_1[16], RB2_1[16]
RB2_7[15],RB2_6[15], RB2_5[15], RB2_4[15], RB2_3[15], RB2_2[15], RB2_1[15], RB2_1[15]
RB2_7[14],RB2_6[14], RB2_5[14], RB2_4[14], RB2_3[14], RB2_2[14], RB2_1[14], RB2_1[14]
RB2_7[13],RB2_6[13], RB2_5[13], RB2_4[13], RB2_3[13], RB2_2[13], RB2_1[13], RB2_1[13]
RB2_7[12],RB2_6[12], RB2_5[12], RB2_4[12], RB2_3[12], RB2_2[12], RB2_1[12], RB2_1[12]
RB2_7[11],RB2_6[11], RB2_5[11], RB2_4[11], RB2_3[11], RB2_2[11], RB2_1[11], RB2_1[11]
RB2_7[10],RB2_6[10], RB2_5[10], RB2_4[10], RB2_3[10], RB2_2[10], RB2_1[10], RB2_1[10]
RB2_7[09],RB2_6[09], RB2_5[09], RB2_4[09], RB2_3[09], RB2_2[09], RB2_1[09], RB2_1[09]
RB2_7[08],RB2_6[08], RB2_5[08], RB2_4[08], RB2_3[08], RB2_2[08], RB2_1[08], RB2_1[08]
RB2_7[07],RB2_6[07], RB2_5[07], RB2_4[07], RB2_3[07], RB2_2[07], RB2_1[07], RB2_1[07]
RB2_7[06],RB2_6[06], RB2_5[06], RB2_4[06], RB2_3[06], RB2_2[06], RB2_1[06], RB2_1[06]
RB2_7[05],RB2_6[05], RB2_5[05], RB2_4[05], RB2_3[05], RB2_2[05], RB2_1[05], RB2_1[05]
RB2_7[04],RB2_6[04], RB2_5[04], RB2_4[04], RB2_3[04], RB2_2[04], RB2_1[04], RB2_1[04]
RB2_7[03],RB2_6[03], RB2_5[03], RB2_4[03], RB2_3[03], RB2_2[03], RB2_1[03], RB2_1[03]
RB2_7[02],RB2_6[02], RB2_5[02], RB2_4[02], RB2_3[02], RB2_2[02], RB2_1[02], RB2_1[02]
RB2_7[01],RB2_6[01], RB2_5[01], RB2_4[01], RB2_3[01], RB2_2[01], RB2_1[01], RB2_1[01]
RB2_7[00],RB2_6[00], RB2_5[00], RB2_4[00], RB2_3[00], RB2_2[00], RB2_1[00], RB2_1[00]


*/
